package leetcode.lt100;

class Solution_98 {

    public static void main(String[] args) {
        new Solution_98();
    }

    public boolean isValidBST(TreeNode root) {
        return root == null || (isValidBSTWithMinAndMax(root.left, Long.MIN_VALUE, root.val) && isValidBSTWithMinAndMax(root.right, root.val, Long.MAX_VALUE));
    }

    private boolean isValidBSTWithMinAndMax(TreeNode root, long minVal, long maxVal) {
        return root == null || (root.val > minVal && root.val < maxVal && isValidBSTWithMinAndMax(root.left, minVal, root.val) && isValidBSTWithMinAndMax(root.right, root.val, maxVal));
    }


}